<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="title" :visible.sync="isShow" width="30%" @close="close">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form
      ref="form"
      label-width="120px"
      :model="formData"
      :rules="formRules"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="formData.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="formData.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="formData.manager"
          style="width: 80%"
          placeholder="请选择"
          @focus="getemployees"
        >
          <el-option
            v-for="item in employeesList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="formData.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button
          type="primary"
          size="small"
          @click="complete"
        >确定</el-button>
        <el-button size="small" @click="isShow = false">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getEmployeeSimpleData } from '@/api/employees'
import {
  getDepartmentListData,
  addDepartmentData,
  getDepartmentInfoData,
  editDepartmentData
} from '@/api/department'
export default {
  props: {
    nodeData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    const checkNameRepeat = async(rules, value, callback) => {
      // 1、找到所有部门数据
      const { depts } = await getDepartmentListData()
      let isRepeat = false
      if (this.formData.id) {
        // 找到兄弟，pid相同且不是自己
        const childArr = depts.filter((item) => item.pid === this.nodeData.pid && item.id !== this.formData.id)
        // 判断是否同名
        isRepeat = childArr.some(item => item.name === value)
      } else {
        // 2、找到当前点击部门所有子部门数据
        const childArr = depts.filter((item) => item.pid === this.nodeData.id)
        // 3、判断是否同名
        isRepeat = childArr.some((item) => item.name === value)
      }

      return isRepeat
        ? callback(new Error(`已存在${value}这个部门`))
        : callback()
    }
    const checkCodeRepeat = async(rules, value, callback) => {
      // 1、找到所有部门数据
      const { depts } = await getDepartmentListData()
      let isRepeat = false
      if (this.formData.id) {
        // 排除自己是否有同名
        isRepeat = depts.some((item) => item.code === value && item.id !== this.formData.id && value)
      } else {
        // 3、判断是否同名
        isRepeat = depts.some((item) => item.code === value)
      }

      return isRepeat
        ? callback(new Error(`已存在${value}这个部门编码`))
        : callback()
    }
    return {
      employeesList: [],
      isShow: false,
      formData: {
        // name: '', // 部门名称
        // code: '', // 部门编码
        // manager: '', // 部门管理者
        // introduce: '' // 部门介绍
      },
      formRules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门名称要求1-50个字符',
            trigger: 'blur'
          },
          {
            validator: checkNameRepeat,
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门编码要求1-50个字符',
            trigger: 'blur'
          },
          {
            validator: checkCodeRepeat,
            trigger: 'blur'
          }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          {
            trigger: 'blur',
            min: 1,
            max: 300,
            message: '部门介绍要求1-300个字符'
          }
        ]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改部门' : '添加部门'
    }
  },
  methods: {
    async getgetDepartmentInfo(id) {
      this.formData = await getDepartmentInfoData(id)
    },
    async getemployees() {
      const res = await getEmployeeSimpleData()
      this.employeesList = res
    },
    async complete() {
      // 1、获取校验结果
      await this.$refs.form.validate()
      if (this.formData.id) {
        await editDepartmentData(this.formData)
      } else {
        await addDepartmentData({ ...this.formData, pid: this.nodeData.id })
      }
      this.$message.success('操作成功')
      this.$emit('update')
      this.isShow = false
    },
    // 弹框关闭触发
    close() {
      console.log('关闭弹窗')
      this.formData = {}
      this.$refs.form.clearvalidate()
    }
  }
}
</script>

<style>
</style>
